#!/usr/bin/env python
# encoding: utf-8
'''
Created on Jun 8, 2011

@author: apple
'''

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import random
import hashlib
import cPickle as pickle
import config

__NAME__ = "USERSESSION@FREEASKER"
__HOSTS__ = ["127.0.0.1:11211"]
__version__ = "0.1"

class Session:
    def __init__(self, validity=1209600):
        self.mc = config.mc
        self.data = dict(islogin=False, email="")
        self.validity = validity
        token = web.cookies().get(__NAME__)
#        web.debug(token)
#        web.debug("---")
        if token:
            self.client = token
        else:
            self.client = hashlib.md5("%s%s" % (hashlib.md5(web.ctx.ip).hexdigest(), hashlib.md5(str(random.randint(0, 10000))).hexdigest())).hexdigest()
            web.setcookie(__NAME__, self.client, expires=self.validity)
#            web.debug("setcookie")

    def __session__(self):
        rawdata = self.mc.get(self.client)
        if not rawdata:
            return dict()
        else:
            return pickle.loads(rawdata)

    def __save__(self, data):
        self.mc.set(self.client, pickle.dumps(data), self.validity)

    def __getitem__(self, key):
        sessions = self.__session__()
        return sessions.get(key, None)

    def __setitem__(self, key, value):
        sessions = self.__session__()
        sessions.__setitem__(key, value)
        self.__save__(sessions)

    def __delitem__(self, key):
        sessions = self.__session__()
        if sessions.has_key(key):
            del sessions[key]
        self.__save__(sessions)

    def keys(self):
        sessions = self.__session__()
        return sessions.keys()

    def values(self):
        return self.__session__().values()

    def items(self):
        return self.__session__().items()

    def get(self, key, default=None):
        sessions = self.__session__()
        return sessions.get(key, default)

    def set(self, key, value, validity=0):
        sessions = self.__session__()
        sessions.__setitem__(key, value)
        if not validity:
            validity = self.validity
        else:
            self.validity = validity
        self.__save__(sessions)

    def delete(self, key):
        self.__delitem__(key)

    def removeall(self):
        self.mc.delete(self.client)

def getlogininfo():
    store = Session()
    return store.get("email", None)

def getnick():
    store = Session()
    return store.get("nick", None)

def getstoriesprepage():
    store = Session()
    return store.get("storiesprepage", None)

def getcommentsprepage():
    store = Session()
    return store.get("commentsprepage", None)

def setlogininfo(email="", nick="", face="", sp=None, cp=None):
    store = Session()
    if email:store.set("email", email)
    if nick:store.set("nick", nick)
    if face:store.set("face", face)
    if sp:store.set("storiesprepage", sp)
    if cp:store.set("commentsprepage", cp)

def destorylogininfo():
    store = Session()
    store.removeall()

def setimagecode(code):
    store = Session()
    store.set("imagecode", code)

def destoryimagecode():
    store = Session()
    store.delete("imagecode")

def checkimagecode(code):
    store = Session()
    return code == store.get("imagecode")
    

if __name__ == "__main__":
    print "This is engiy memcache session,version is %s" % __version__
